/*
 * Copyright 2006-2008 Martin Konzett <martin.konzett AT gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.toools.gwt.wmc.client.additive.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.toools.gwt.widget.client.cloud.*;
import org.toools.gwt.widget.client.bc.*;
import org.toools.gwt.wmc.client.additive.AdditiveService;
import org.toools.gwt.wmc.client.companypart.CompanyPartService;
import org.toools.gwt.wmc.jpa.Additive;
import org.toools.gwt.wmc.jpa.CompanyPart;

public class AdditiveListRenderer extends BasicListRenderer {

    private Additive add = null;

    BasicTogglePanel companyparts = new BasicTogglePanel("set companyparts", 1, true) {
        public void onActive() {
            clear();
            addChild(new CompanyPartsRelation((Additive) getTag().getAttachedBusinessObject()));
        }
    };

    public AdditiveListRenderer(TagVO tag, BasicControl bc) {
		super(tag, bc);

        add = (Additive)tag.getAttachedBusinessObject();

//        questionable if this should be done here
//        addButton(companyparts);
	}
	public void setTag(TagVO tag) {
		Additive ac = (Additive) tag.getAttachedBusinessObject();
		setTitle(ac.getContent());
        if(ac.getCompanyParts() != null) {
            if(ac.getCompanyParts().size() == 0) {
                this.setSubTitle("no companyparts");
            } else if(ac.getCompanyParts().size() == 1) {
                this.setSubTitle("one companypart");
            } else if(ac.getCompanyParts().size() > 1) {
                this.setSubTitle("" + ac.getCompanyParts().size() + " companyparts");
            }
        }
	}

    private class CompanyPartsRelation extends RelationControl {
        Additive additive;
        public CompanyPartsRelation(Additive a) {
            super(false, true, true, false);
            this.additive = a;
            CompanyPartService.Util.getInstance().getCompanyParts(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }

        public TagVO getTagVO(Object bp) {
            CompanyPart cp = (CompanyPart) bp;
            TagVO result = new TagVO("" + cp.getId(), cp.getName(), 0, 0);
            result.setAttachedBusinessObject(cp);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<CompanyPart>)result).toArray()));
                if((add.getCompanyParts() != null) && (add.getCompanyParts().size() > 0)) {
                    String[] active = new String[add.getCompanyParts().size()];
                    int j = 0;
                    for(Iterator i = add.getCompanyParts().iterator(); i.hasNext();) {
                        CompanyPart cp = (CompanyPart) i.next();
                        active[j++] = "" + cp.getId();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                List<CompanyPart> cps = new ArrayList<CompanyPart>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    cps.add((CompanyPart)tag.getAttachedBusinessObject());
                }
                additive.setCompanyParts(cps);
                AdditiveService.Util.getInstance().update(additive, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }
}